<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
import { patientListApi, removePatientApi } from "@/apis/patient";
import { useConsultStore } from "@/stores/consult";
import type { PatientItem } from "@/types/patient";
import { onPageShow } from "@dcloudio/uni-app";
import { ref } from "vue";

// <uni-swipe-action-item> 侧滑删除配置
const swipeOptions = ref([
  {
    text: "删除",
    style: {
      backgroundColor: "#dd524d",
      color: "",
      fontSize: "",
    },
  },
]);

// 列表数据
const list = ref<PatientItem[]>([]);
// 获取列表数据
const getData = async () => {
  const res = await patientListApi();
  list.value = res;
  // 默认患者
  const defaultPatient = list.value.find((item) => item.defaultFlag === 1);
  if (defaultPatient) {
    // 保存默认患者
    store.patientId = defaultPatient.id;
  }
};

// 页面显示时刷新数据，因为新增后会返回列表页，所以要刷新数据
onPageShow(() => {
  getData();
});

// 侧滑删除时保存 id
const activeId = ref("");
// 点击删除按钮
const onDeleteItem = async (id: string) => {
  // 保存id
  activeId.value = id;
  // 二次确认弹窗
  const res = await uni.showModal({
    title: "温馨提示",
    content: "确定删除该患者吗？",
  });
  // 清除 id
  activeId.value = "";
  if (res.confirm) {
    // 发送请求让后端删除患者信息
    await removePatientApi(id);
    // 本地删除
    list.value = list.value.filter((item) => item.id !== id);
    // 轻提示
    uni.showToast({ title: "删除成功", icon: "none" });
    // 如果当前页面选中的 id 和删除的 id 相同，则清除患者 id
    if (store.patientId === id) {
      store.patientId = "";
    }
  }
};
// 去支付页
const gotoPayment = () => {
  uni.navigateTo({ url: "/subpkg_consult/payment/index" });
};

// ---------------- 选择患者 ----------------
// 获取页面参数
const pageParams = defineProps<{
  type?: "consult";
}>();

uni.setNavigationBarTitle({
  title: pageParams.type === "consult" ? "选择患者" : "档案列表",
});

const store = useConsultStore();
const selectPatient = (id: string) => {
  if (pageParams.type === "consult") {
    store.patientId = id;
  }
};
// ---------------- 选择患者 ----------------
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view v-if="pageParams.type === 'consult'">
        <view>请选择患者信息</view>
        <view class="archive-tips"
          >以便医生给出更准确的治疗，信息仅医生可见</view
        >
      </view>
      <view v-else class="archive-tips">最多可添加6人</view>
      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="item in list"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onDeleteItem(item.id)"
          :show="activeId === item.id ? 'right' : 'none'"
        >
          <view
            class="archive-card"
            @click="selectPatient(item.id)"
            :class="{
              active:
                pageParams.type === 'consult' && store.patientId === item.id,
            }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">
                {{ item.idCard.replace(/^(.{6}).+(.{4})$/, "$1*********$2") }}
              </text>
              <!-- 嵌套的 default -->
              <text v-if="item.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender === 1 ? "男" : "女" }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="list.length < 6" class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/from/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <button
        @click="gotoPayment"
        v-if="pageParams.type === 'consult'"
        :disabled="!store.patientId"
        class="uni-button"
      >
        下一步
      </button>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import "./index.scss";
</style>
